package com.huawei.appgallery.wlanapp.ui;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.huawei.appgallery.assistantdock.base.externalaction.openviewaction.RemoteBuoyAction;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.service.common.protocol.AppListFragmentProtocol;
import com.huawei.appgallery.foundation.service.common.protocol.request.AppListFragmentRequest;
import com.huawei.appgallery.foundation.store.bean.detail.DetailResponse;
import com.huawei.appgallery.foundation.ui.framework.cardframe.fragment.TaskFragment;
import com.huawei.appgallery.foundation.ui.framework.cardframe.fragment.TaskFragment.e;
import com.huawei.appgallery.foundation.ui.framework.fragment.LoadingFragment;
import com.huawei.appgallery.foundation.ui.framework.titleframe.bean.BaseTitleBean;
import com.huawei.appgallery.foundation.ui.framework.uikit.ContractFragment;
import com.huawei.appgallery.wlanapp.api.IWlanAppListActivityProtocol;
import com.huawei.appgallery.wlanapp.ui.card.WlanAppBean;
import com.huawei.appgallery.wlanapp.ui.card.WlanAppNode;
import com.huawei.hmf.annotation.ActivityDefine;
import com.huawei.secure.android.common.intent.SafeBundle;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import o.azb;
import o.ccv;
import o.cdh;
import o.cdk;
import o.cfv;
import o.cgl;
import o.chm;
import o.cib;
import o.cin;
import o.cir;
import o.cjd;
import o.cje;
import o.cjf;
import o.cjk;
import o.cjl;
import o.cjm;
import o.cjo;
import o.cvt;
import o.deg;
import o.deh.a;
import o.deh.b;
import o.deh.c;
import o.del;
import o.den;
import o.dep;
import o.ActivityUtil;
import o.fiv;
import o.FragmentTransaction;

@ActivityDefine(alias = "wlanzone", protocol = IWlanAppListActivityProtocol.class)
public class WlanAppListActivity extends FragmentActivity implements e {
    private String ˊॱ;
    private String ˋॱ;
    private String ˏॱ;
    private String ͺ;
    private cjf ॱˊ;
    private fiv ॱˋ = fiv.ॱ(this);
    private BroadcastReceiver ॱᐝ = new BroadcastReceiver(this) {
        String ˊ = "reason";
        final /* synthetic */ WlanAppListActivity ˋ;
        String ˏ = "homekey";

        {
            this.ˋ = r2;
        }

        public void onReceive(Context context, Intent intent) {
            SafeIntent safeIntent = new SafeIntent(intent);
            if (!"android.intent.action.CLOSE_SYSTEM_DIALOGS".equals(safeIntent.getAction())) {
                Bundle extras = safeIntent.getExtras();
                if (extras != null) {
                    int i = extras.getInt("downloadtask.status", -1);
                    SessionDownloadTask ˊ = SessionDownloadTask.ˊ(safeIntent.getBundleExtra("downloadtask.all"));
                    if (ˊ != null) {
                        switch (i) {
                            case 0:
                                den.ˊ(context, ˊ.ᐝ(), 1, ˊ.ॱˋ());
                                return;
                            case 2:
                                return;
                            case 3:
                            case 5:
                            case 6:
                                den.ˊ(context, ˊ.ᐝ(), 4, ˊ.ॱˋ());
                                return;
                            default:
                                return;
                        }
                    }
                }
            } else if (TextUtils.equals(safeIntent.getStringExtra(this.ˊ), this.ˏ)) {
                this.ˋ.ͺ();
            }
        }
    };

    static {
        cjl.ˋ("loading.fragment", LoadingFragment.class);
        cjl.ˋ("wlanapplist.fragment", WlanAppListFragment.class);
        cib.ˎ("wlanappcard", WlanAppNode.class);
        cib.ˊ("wlanappcard", WlanAppBean.class);
    }

    public void onCreate(Bundle bundle) {
        del.ॱ();
        super.onCreate(bundle);
        chm.ॱ().ˋ();
        setContentView(c.ॱ);
        ˊॱ();
        Bundle bundle2 = new Bundle();
        if (bundle == null) {
            IWlanAppListActivityProtocol iWlanAppListActivityProtocol = (IWlanAppListActivityProtocol) this.ॱˋ.ॱ();
            if (iWlanAppListActivityProtocol != null) {
                this.ͺ = iWlanAppListActivityProtocol.getUri();
                this.ˊॱ = iWlanAppListActivityProtocol.getAppKey();
                this.ˏॱ = iWlanAppListActivityProtocol.getIv();
                this.ˋॱ = iWlanAppListActivityProtocol.getAId();
            }
            if (this.ˊॱ != null) {
                dep.ˊ().ˋ(this.ˊॱ, this.ˏॱ);
            } else {
                deg.ˏ.ˊ("WlanAppListActivity", "appKey is null!");
            }
        } else {
            this.ͺ = bundle.getString("WlanAppZoneActivity.Card.URI");
            this.ˊॱ = bundle.getString("WlanAppZoneActivity.Card.appKey");
            this.ˏॱ = bundle.getString("iv");
            this.ˋॱ = bundle.getString("WlanAppZoneActivity.Card.aId");
            dep.ˊ().ˋ(this.ˊॱ, this.ˏॱ);
        }
        if (this.ͺ == null || this.ˋॱ == null) {
            deg.ˏ.ˏ("AppDetailActivity", "Cannot get URI from intent's param WlanAppZoneActivity.Card.URI");
            finish();
        } else {
            bundle2.putString(RemoteBuoyAction.REMOTE_BUOY_URI, this.ͺ);
            bundle2.putString("aid_tag", this.ˋॱ);
            Fragment ˊ = cjk.ˎ().ˊ(new cjm("loading.fragment", null));
            if (ˊ instanceof TaskFragment) {
                TaskFragment taskFragment = (TaskFragment) ˊ;
                taskFragment.ʼ(bundle2);
                taskFragment.ˋ(ˏॱ(), b.ˊ, "TaskFragment");
            }
        }
        ᐝ();
    }

    public void onSaveInstanceState(Bundle bundle) {
        if (!TextUtils.isEmpty(this.ͺ) && !TextUtils.isEmpty(this.ˊॱ) && !TextUtils.isEmpty(this.ˏॱ) && !TextUtils.isEmpty(this.ˋॱ)) {
            bundle.putString("WlanAppZoneActivity.Card.URI", this.ͺ);
            bundle.putString("WlanAppZoneActivity.Card.appKey", this.ˊॱ);
            bundle.putString("iv", this.ˏॱ);
            bundle.putString("WlanAppZoneActivity.Card.aId", this.ˋॱ);
            super.onSaveInstanceState(bundle);
        }
    }

    private void ᐝ() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ccv.ˋ());
        intentFilter.addAction(ccv.ˊ());
        intentFilter.addAction("android.intent.action.CLOSE_SYSTEM_DIALOGS");
        ActivityUtil.registerReceiver(this, intentFilter, this.ॱᐝ);
    }

    private void ʼ() {
        ActivityUtil.unregisterReceiver((Context) this, this.ॱᐝ);
    }

    public void onDestroy() {
        cdh.ॱ(null);
        ʼ();
        super.onDestroy();
    }

    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (4 == keyEvent.getKeyCode()) {
            finish();
        }
        return false;
    }

    public boolean ˋ(TaskFragment taskFragment, TaskFragment.b bVar) {
        if (bVar.ˋ.getResponseCode() == 0 && bVar.ˋ.getRtnCode_() == 0) {
            SafeBundle safeBundle = new SafeBundle(taskFragment.ˊॱ());
            AppListFragmentRequest appListFragmentRequest = new AppListFragmentRequest();
            appListFragmentRequest.ˋ(safeBundle.getString(RemoteBuoyAction.REMOTE_BUOY_URI));
            appListFragmentRequest.ˊ(safeBundle.getString("trace_id"));
            appListFragmentRequest.ॱ(1);
            appListFragmentRequest.ˊ(0);
            appListFragmentRequest.ˎ(safeBundle.getString("aid_tag"));
            appListFragmentRequest.ˊ(false);
            cjo appListFragmentProtocol = new AppListFragmentProtocol();
            appListFragmentProtocol.ˏ(appListFragmentRequest);
            ContractFragment contractFragment = (ContractFragment) cjk.ˎ().ˊ(new cjm("wlanapplist.fragment", appListFragmentProtocol));
            if (!(contractFragment == null || contractFragment.ˊ(cin.class) == null)) {
                ((cin) contractFragment).ॱ(bVar);
            }
            if (bVar.ˋ instanceof DetailResponse) {
                setTitle(((DetailResponse) bVar.ˋ).ˋ());
            }
            FragmentTransaction ˊ = ˏॱ().ˊ();
            ˊ.ˏ(b.ˊ, contractFragment, "AppDetail");
            ˊ.ˋ();
            return true;
        }
        if (taskFragment != null) {
            cir o_cir = (cir) taskFragment.ˊ(cir.class);
            if (o_cir != null) {
                o_cir.ॱ(bVar.ˋ.getResponseCode(), true);
            }
        }
        return false;
    }

    public void ˎ(TaskFragment taskFragment, List<cvt> list) {
        SafeBundle safeBundle = new SafeBundle(taskFragment.ˊॱ());
        cfv ˎ = cfv.ˎ(safeBundle.getString(RemoteBuoyAction.REMOTE_BUOY_URI), safeBundle.getString("trace_id"), cdh.ˎ(this), 1);
        ˎ.ॱ(safeBundle.getString("aid_tag"));
        list.add(ˎ);
    }

    public void onResume() {
        super.onResume();
        cdk.ˊ(getClass().getCanonicalName(), ॱॱ());
    }

    public void onPause() {
        super.onPause();
        cdk.ˏ(getClass().getCanonicalName(), ॱॱ());
    }

    private LinkedHashMap ॱॱ() {
        LinkedHashMap linkedHashMap = new LinkedHashMap(3);
        if (cdh.ˏ() != null) {
            linkedHashMap.put("third_id", cdh.ˏ());
        }
        if (!TextUtils.isEmpty(this.ͺ)) {
            linkedHashMap.put("page_id", this.ͺ);
        }
        linkedHashMap.put("service_type", Integer.valueOf(cdh.ˎ(this)));
        return linkedHashMap;
    }

    private void ͺ() {
        List<RunningTaskInfo> runningTasks = ((ActivityManager) getSystemService("activity")).getRunningTasks(1000);
        if (runningTasks != null && !runningTasks.isEmpty()) {
            for (RunningTaskInfo runningTaskInfo : runningTasks) {
                if ("com.huawei.appmarket.MarketActivity".equals(runningTaskInfo.baseActivity.getClassName())) {
                    Intent intent = new Intent();
                    intent.addCategory("android.intent.category.HOME");
                    intent.setAction("android.intent.action.MAIN");
                    startActivity(intent);
                    break;
                }
            }
        }
        finish();
    }

    private void ˊॱ() {
        View findViewById = findViewById(b.ˏॱ);
        azb.ˏ(findViewById);
        if (findViewById instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) findViewById;
            String toUpperCase = cgl.ॱ(getString(a.ˏ)).toUpperCase(Locale.getDefault());
            BaseTitleBean baseTitleBean = new BaseTitleBean();
            baseTitleBean.ॱ(toUpperCase);
            cje o_cje = new cje();
            o_cje.ˎ(baseTitleBean);
            o_cje.ˋ("back_title");
            this.ॱˊ = cjd.ˋ(this, o_cje);
            View view = null;
            if (this.ॱˊ != null) {
                view = this.ॱˊ.ˋ();
            }
            if (view != null) {
                viewGroup.setVisibility(0);
                viewGroup.addView(view);
            }
        }
    }

    public void setTitle(CharSequence charSequence) {
        super.setTitle(charSequence);
        ˎ(charSequence);
    }

    public void setTitle(int i) {
        super.setTitle(i);
        ˎ(getString(i));
    }

    private void ˎ(CharSequence charSequence) {
        if (this.ॱˊ != null && charSequence != null) {
            BaseTitleBean ˎ = this.ॱˊ.ˎ();
            if (ˎ != null) {
                ˎ.ॱ(charSequence.toString());
                this.ॱˊ.ॱ(ˎ);
            }
        }
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != 16908332) {
            return super.onOptionsItemSelected(menuItem);
        }
        finish();
        return true;
    }
}
